/**
 * class name: ipop,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/core.js, mask.js,
 * update: http://jsease.googlecode.com/svn/trunk/ipop.js
 *
 */
function ipop(){/* {id,css,ico,title,x,data} */
    var A = arguments,
        _ = this;
    _ = ext( _, ext({
        id : 'ipop'+ now(3),
        css : 'position:fixed;top:0;left:0;',
        ico : {src:'',css:''},
        width:300,
        height:300,
        close : '/icos/iclose.gif',
        title : '',
        nomask : false,
        maskcolor : '#000',
        maskopacity : 40,
        notitle : false,
        noclose : false,
        autoresize : true,
        autocenter : true,
        radius : 3,
        data : '',
        z : now().getFullYear()+1
        }, A[0] ) );

    _.uitype = 'ipop';

    _.hide = function(){
        _.mask && _.mask.hide();
        _.$.style.display = 'none';
    return _;
    };
    _.show = function(msg){
        if ( typeof msg=='object' ) {
            if ( hop(msg, 'ico') ) {
                _.ico.src = msg.ico;
            }

            if ( hop(msg, 'title') ) {
                _.title = msg.title;
            }

            if ( hop(msg, 'css') ) {
                _.css = msg.css;
                _.$.style.cssText = _.css;
            }
        }
        msg.data && _.setContent(msg.data);
        _.mask && _.mask.show();
        _.$.style.display = '';
        _.autoresize && msg.data && _.autosize(msg.data);
    return _;
    };

    _.move = function(ev){
        var ev = evt(ev);
        _.pz.left = ev.X-_.pz._x<=0 || _.pz.W<=_.pz.width ? 0
            : ev.X-_.pz._x+_.pz.width>=_.pz.W ? _.pz.W-_.pz.width
                : ev.X-_.pz._x;
        _.pz.top = ev.Y-_.pz._y<=0 || _.pz.H<=_.pz.height ? 0
            : ev.Y-_.pz._y+_.pz.height>=_.pz.H ? _.pz.H-_.pz.height
                : ev.Y-_.pz._y;

        _.$.style.top = _.pz.top +'px';
        _.$.style.left = _.pz.left +'px';

        me.all && _.ititle.setCapture();

    };

    _.stop = function(){
        try {
            unbind(me, 'mousemove', _.move);
            unbind(me, 'mouseup', _.stop);
            window.getSelection && window.getSelection().removeAllRanges();
            me.all && _.ititle.releaseCapture();
            if ( $$.msie ) {
                me.onselectstart = function(){;};
            }
        } catch (e) {}
    };

    _.tomove = function(ev){
        if ( $$.msie ) {
            me.onselectstart = function(){
                me.selection.empty();
                event.returnValue = false;
            };
        }

        _.pz = pos(_.$);
        var ev = evt(ev);
        _.pz._y = ev.Y-_.pz.top;
        _.pz._x = ev.X-_.pz.left;

        bind(me, 'mousemove', _.move);
        bind(me, 'mouseup', _.stop);
    };

    _.setpos = function(){
        _.pz = pos(_.$);

        if ( _.autocenter ) {
            _.$.style.top = _.pz.H<=_.pz.height ? 0 : (_.pz.H-_.pz.height)/2 +'px';
            _.$.style.left = _.pz.W<=_.pz.width ? 0 : (_.pz.W-_.pz.width)/2 +'px';
            _.pz = pos(_.$);
        }

    return _;
    };

    _.autosize = function (msg) {
        if ( msg.nodeType ) {
            var mz = pos(msg);

            _.$.style.width = (mz.width + _.radius*2) +'px';
            _.$.style.height = (mz.height + _.radius*2 + (!_.notitle ? pos(_.ititle).height : 0)) +'px';
            _.body.style.padding = _.radius +'px';
        }

        _.setpos();

    return _;
    };

    _.setContent = function(msg){
        if ( !_.notitle ) {
            _.titletxt.empty().html(_.title);
            if (p(_.ico,'src') ) {
                _.icon.style.backgroundImage = 'url('+ _.ico.src +')';
            }
        }


        if ( typeof msg =='function' ) {
            _.htm = msg.apply(_.body, [_.$]);
            if ( typeof _.htm=='string' ) {
                _.body.html( _.htm );
            }
        } else if ( typeof msg=='string' ) {
            _.body.html( msg );
        } else if ( msg.nodeType ) {
            var dp = $('datapool');
            if ( !dp ) {
                var dp = elm('div').attr('id','datapool').appendTo(me.body).attr('style', 'display:none;')
            }
            while ( _.body.firstChild ) {
                dp.appendChild(_.body.firstChild);
            }
            _.body.appendChild(msg);
        }

    return _;
    };

    _.init = function(){
        _.$ = elm('div').before(me.body.childNodes[0]).setClass('ipop');
        _.id = _.$.id = p(_, 'id') && !$(_.id) ? _.id : ('ipop_'+ now(3));
        _.$.style.cssText = _.css;
        _.$.style.zIndex = _.z;
        if (!_.autoresize) {
            _.$.style.width = _.width +'px';
            _.$.style.height = _.height +'px';
        }

        _.setpos();

        if ( !_.nomask ) {
            _.mask = new mask({id:(_.id +'mask'), opacity:_.maskopacity||50, color:_.maskcolor||'#fff', z: _.z-1}).init().hide();
        }

        if ( !_.notitle ) {
            /* popup title */
            _.ititle = elm('div').setClass('ititle').appendTo(_.$);
            _.ititle.style.zIndex = _.z;
            bind(_.ititle, 'mousedown', _.tomove);

            _.icon = elm('div').setClass('ico').appendTo(_.ititle);
            if ( p(_.ico,'src') ) {
                _.icon.style.backgroundImage = 'url('+ _.ico.src +')';
            }

            _.titletxt = elm('div').setClass('ititle-text').html(_.title||'').appendTo(_.ititle);

            _.icon.onmousedown = _.titletxt.onmousedown = function() {
                me.selection && me.selection.empty();
                window.getSelection && window.getSelection().removeAllRanges();
                return false;
            };

            if ( !_.noclose ) {
                _.xclose = elm('div').setClass('iclose').appendTo(_.$);

                if ( _.close ) {
                    _.xclose.style.cssText = 'background:url('+ _.close +') no-repeat;';
                }

                _.xclose.onmouseover = function(){
                    this.style.backgroundPosition = '0 -16px';
                };

                _.xclose.onmouseout = function(){
                    this.style.backgroundPosition = '0 0';
                };

                _.xclose.onclick = function(){
                    _.hide();
                };

            }
        }

        /* popup body */
        _.body = elm('div').setClass('icontent').appendTo(_.$);
        _.body.style.zIndex = _.z;

        _.setContent(_.data);

    return _.hide();
    };

}